<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+

class TimeUtil
{

	// return array with hours in format 00
	function setHours($min = 0, $max = 24) {
		for($i=$min;$i<=$max;$i++){
			$val = ($i<10) ? '0'.$i : $i;
			$ar[$val] = $i;
		}
		return $ar;
	}
	
	
	// return array with minutes in format 00
	function setMinutes($min, $max, $gap) {
		for($i=$min;$i<=$max;$i+=$gap){
			
			$val = ($i<10) ? '0'.$i : $i;
			$ar[$val] = $val;
		}
		return $ar;
	}
	
	// return whole hours from minutes
	function getHours($minutes) {
		$hour = (int) ($minutes / 60);
		$hour = ($hour < 10) ? '0'.$hour : $hour;
		
		return $hour;
	}
	
	// return minutes after whole hours 
	function getMinutes($minutes) {
		$minutes = (int) ($minutes % 60);
		$minutes = ($minutes < 10) ? '0'.$minutes : $minutes;
		
		return $minutes;
	}
	
	
	function toMinites($hours, $minutes) {
		$hour = (int) ($hours * 60);
		$value = (int) $hour + $minutes;
		
		return $value;
	}


	function toStampFromSqlDate($date) {
		
		$date = str_replace('-', '', $date);
		
		$year = substr($date, 0, 4);
		$month = substr($date, 4, 2);
		$day = substr($date, 6);
		
		$str =  mktime (0,0,0,$month,$day,$year);
		return $str;	
	}
	
	
	// from that '15 March, 2004' to UNIXTIMESTAMP
	// if change something also do changes to getFormatedDate($timestamp)
	function toStampFromJsDate($date) {
		$date = str_replace(',', '', $date);
		$date = explode(' ', $date);
		
		for($i=1;$i<=12;$i++){
			$monthes[strftime('%B', mktime(0,0,0,$i,1,2004))] = $i;
		}
		
		$hour = date('H');
		$min = date('i');
		$sek = date('s');
		$month = $monthes[$date[1]];
		$date = mktime($hour,$min,$sek,$month,$date[0],$date[2]);
		
		return $date;
	}
	
	
	function toFormatedFromStamp($timestamp) {
		$date = strftime('%d %B, %Y', $timestamp);
		return $date;
	}
	
	function toSqlFromStamp($timestamp) {
		$date = date('YmdHis', $timestamp);
		return $date;
	}
	
	
	//Two usefull functions, if you want to built your own calendar.
	# Returns date of the Monday for given number of week(1..53) and  year.
	# Output is in date-format.
	function _getMonday($week, $year=""){
		$first_date = strtotime("1 january ".($year ? $year : date("Y")));
		if(date("D", $first_date)=="Mon") {
			$monday = $first_date;
		} else {
			$monday = strtotime("next Monday", $first_date)-604800;
		}
		
		$plus_week = "+".($week-1)." week";
		return strtotime($plus_week, $monday);
	}
	
	// return timestamp for week start
	function getWeekStart($week, $year, $week_start = 0){
		if($week_start == 0) {
			return TimeUtil::_getMonday($week, $year)-86400;
		} else {
			return TimeUtil::_getMonday($week, $year);
		}
	}
	
	// get days quantity of month
	function getMonthDays($month, $year) {
		return date('j', mktime(0,0,0,$month+1,0,$year));
	}
	
}
?>
